package com.hollysys.hollimsa.biz.istio.gateway.dao;


import com.hollysys.hollimsa.biz.istio.gateway.domain.GatewayDTO;
import com.hollysys.hollimsa.biz.istio.gateway.domain.GatewayVO;
import me.snowdrop.istio.api.networking.v1beta1.*;
import me.snowdrop.istio.client.IstioClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.Arrays;
import java.util.List;
import java.util.stream.Collectors;

@Repository
public class GatewayDao {

    @Autowired
    private IstioClient istioClient;

    public Gateway createistioGateWay(GatewayVO gatewayVO,String namespace) {
        Server[] servers = Arrays.stream(gatewayVO.getServers()).map(svc -> {
            Port port = new PortBuilder().withName(svc.getPort().getName()).withNumber(svc.getPort().getNumber()).withProtocol(svc.getPort().getProtocol()).build();
            return new ServerBuilder().addToHosts(svc.getHosts()).withPort(port).build();
        }).toArray(Server[]::new);

        final Gateway gateway = new GatewayBuilder()
            .withNewMetadata()
            .withName(gatewayVO.getName())
            .withNamespace(namespace)
            .endMetadata()
            .withNewSpec()
            .addToSelector("istio", "ingressgateway")// StringUtils.isBlank(gatewayVO.getNode()) ? "ingressgateway" : gatewayVO.getNode()

            .addToServers(servers)
            .endSpec()
            .build();
        return istioClient.v1beta1Gateway().inNamespace(namespace).create(gateway);
    }


    public Gateway editistioGateWay(GatewayVO gatewayVO,String namespace) {
        Server[] servers = Arrays.stream(gatewayVO.getServers()).map(svc -> {
            Port port = new PortBuilder().withName(svc.getPort().getName()).withNumber(svc.getPort().getNumber()).withProtocol(svc.getPort().getProtocol()).build();
            return new ServerBuilder().addToHosts(svc.getHosts()).withPort(port).build();
        }).toArray(Server[]::new);

        final Gateway gateway = new GatewayBuilder()
            .editOrNewMetadata()
            .withName(gatewayVO.getName())
            .withNamespace(namespace)
            .endMetadata()
            .editOrNewSpec()
            .addToSelector("istio", "ingressgateway")// StringUtils.isBlank(gatewayVO.getNode()) ? "ingressgateway" : gatewayVO.getNode()
            .addToServers(servers)
            .endSpec()
            .build();

        return istioClient.v1beta1Gateway().inNamespace(namespace).createOrReplace(gateway);
    }


    /**
     * @param name 网关名称
     * @return 返回状态
     */
    public boolean deleteGateway(String name,String namespace) {
        return istioClient.v1beta1Gateway().inNamespace(namespace).withName(name).delete();
    }


    /**
     * 获取网关详情，用于编辑回显
     * @return
     */
    public List<GatewayDTO> listGateway(String namespace) {
        return istioClient.v1beta1Gateway().inNamespace(namespace).list().getItems().stream().map(gateway -> {

            GatewayDTO dto = new GatewayDTO();
            dto.setName(gateway.getMetadata().getName());
            dto.setSelector(gateway.getSpec().getSelector());

            GatewayDTO.InnerServer[] innerServers = gateway.getSpec().getServers().stream().map(server -> {
                GatewayDTO.InnerServer innerServer = new GatewayDTO.InnerServer();
                innerServer.setHosts(server.getHosts().toArray(new String[0]));
                GatewayDTO.InnerPort innerPort =  GatewayDTO.InnerPort.builder().name(server.getPort().getName()).protocol(server.getPort().getProtocol()).number(server.getPort().getNumber()).build();
                innerServer.setPort(innerPort);
                return innerServer;
            }).toArray(GatewayDTO.InnerServer[]::new);
            dto.setServers(innerServers);

            return dto;
        }).collect(Collectors.toList());
    }

    public List<Gateway> gatewayDropDownData(String namespace) {
        return istioClient.v1beta1Gateway().inNamespace(namespace).list().getItems();
    }
}
